layui.use(['form', 'admin', 'ax', 'laydate', 'upload', 'formSelects'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;

    // var exchangeDate = Feng.getUrlParam("exchangeDate")
    // var exchangeId = Feng.getUrlParam("exchangeId")
    // console.log(exchangeId)

    // $("#exchangeDate").val(exchangeDate)
    // $("#exchangeId").val(exchangeId)

    /**
     * 获取上一个页面的服务器详情
     */
        // var res;
        // $.ajax(Feng.ctxPath + '/databaseListKh/searchServer', {
        //     data: {
        //         id: exchangeId
        //     },
        //     success: function (data) {
        //         res = data[0];
        //         console.log(res)
        //     }
        // })


        //获取详情信息，填充表单
    var ajax = new $ax(Feng.ctxPath + "/databaseListKh/getUser");
    var result = ajax.start();
    $("#department").text(result.data.deptName);


    $("#testConnect").click(function () {
        // alert("测试连接")
        $.ajax(Feng.ctxPath + "/databaseListKh/testConnect", {
            data: {
                dbName: $("#dbName").val(),
                username: $("#username").val(),
                password: $("#password").val(),
                dbNumber: $("#dbNumber").val(),
                dbUrl: $("#dbUrl").val(),
                exchangeDate: $("#exchangeDate").val(),
                system: $("#system").val(),
                servicetype: $("#servicetype").val(),
                standard: $("#standard").val(),
                dbMemory: $("#dbMemory").val(),
                effectiveTime: $("#effectiveTime").val(),
                details: $("#details").val(),
                dbType: $("#dbType").val(),
                // exchangeId: exchangeId,
                // exchangeLoc: res.exchangeLoc,
                // exchangeUser: res.exchangeUser,
                // exchangePassword: res.exchangePassword
            },
            success: function (data) {
                let arr = data.split(",")
                //arr一定是length为2的数组  arr[0]代表ping服务器返回值 1代表能访问、2代表不能访问；arr[1] 是连接masql数据库返回值 1代表能连接、2代表不能连接
                if (arr[0] == 1 && arr[1] == 1) {
                    Feng.success("<span style='color: #009688'>连接服务器成功</span><span style='padding: 0 5px 0 5px'>|</span><span style='color: #009688'>连接数据库成功</span>")
                } else if (arr[0] == 1 && arr[1] == 2) {
                    Feng.error("<span style='color: #009688'>连接服务器成功</span><span style='padding: 0 5px 0 5px'>|</span><span style='color: red'>连接数据库失败</span>")
                } else if (arr[0] == 2 && arr[1] == 1) {
                    Feng.error("<span style='color: red'>连接服务器失败</span><span style='padding: 0 5px 0 5px'>|</span><span style='color: #009688'>连接数据库成功</span>")
                } else {
                    Feng.error("<span style='color: red'>连接服务器失败</span><span style='padding: 0 5px 0 5px'>|</span><span style='color: red'>连接数据库失败</span>")
                }
            },
        })
    })

    $("#returnServiceList").click(function () {
        history.go(-1)
    })


    //表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        // console.log(data.field)
        data.field.department = $("#department").text();
        var ajax = new $ax(Feng.ctxPath + "/databaseListKh/addItem", function (data) {
            if (data.code == 200) {
                Feng.success("注册成功<span style='padding: 0 5px 0 5px'>|</span>等待审批");
                //传给上个页面，刷新table用
                admin.putTempData('formOk', true);
                //关掉对话框
                // admin.closeThisDialog();
                history.go(-1)
            } else if (data.code == 500) {
                Feng.error(data.message);
            }

        }, function (data) {
            Feng.error("添加失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        return false;
    });


});